﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class CatalogSupplierTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [Test]
        public void Can_CRUD()
        {
            var supplier = Processor.Execute(new CreateCatalogSupplier
            {
                FileGroupID = 0
            }).Result;

            Assert.NotNull(supplier);
            supplier.AreEqual(Processor.Execute(new GetCatalogSupplier
            {
                EntityID = supplier.EntityID
            }).Result);

            var upd_supplier = Processor.Execute(new UpdateCatalogSupplier
            {
                EntityID = supplier.EntityID,
                FileGroupID = 0,
                Flags = CatalogSupplierFlags.Active
            }).Result;

            Assert.NotNull(upd_supplier);
            upd_supplier.AreEqual(supplier);
            upd_supplier.Flags.AreEqual(CatalogSupplierFlags.Active);

            Processor.Execute(new DeleteCatalogSupplier
            {
                EntityID = supplier.EntityID
            });
        }

        [Test]
        public void Can_Manage_Tag_Relations()
        {
            var tag = Processor.Execute(new CreateCatalogTag()).Result;
            var supplier = Processor.Execute(new CreateCatalogSupplier
            {
                FileGroupID = 0
            }).Result;

            Processor.Execute(new MapCatalogTagToCatalogSupplier
            {
                ChildID = tag.EntityID,
                EntityID = supplier.EntityID
            }).Result.Tags
                .DoesContain(tag);

            Processor.Execute(new UnmapCatalogTagFromCatalogSupplier
            {
                ChildID = tag.EntityID,
                EntityID = supplier.EntityID
            }).Result.Tags
                .DoesNotContain(tag);
        }

        [Test]
        public void Can_Manage_Category_Relations()
        {
            var category = Processor.Execute(new CreateCatalogCategory()).Result;
            var supplier = Processor.Execute(new CreateCatalogSupplier
            {
                FileGroupID = 0
            }).Result;

            Processor.Execute(new MapCatalogCategoryToCatalogSupplier
            {
                ChildID = category.EntityID,
                EntityID = supplier.EntityID
            }).Result.Categories
                .DoesContain(category);

            Processor.Execute(new UnmapCatalogCategoryFromCatalogSupplier
            {
                ChildID = category.EntityID,
                EntityID = supplier.EntityID
            }).Result.Categories
                .DoesNotContain(category);
        }

        [Test]
        public void Can_Manage_Attribute_Relations()
        {
            var attribute = Processor.Execute(new CreateLookupAttribute
            {
                Flags = LookupAttributeFlags.CatalogSupplier
            }).Result;
            var supplier = Processor.Execute(new CreateCatalogSupplier
            {
                FileGroupID = 0
            }).Result;

            Processor.Execute(new MapLookupAttributeToCatalogSupplier
            {
                ChildID = attribute.EntityID,
                EntityID = supplier.EntityID
            }).Result.Attributes
                .DoesContain(attribute);

            Processor.Execute(new UnmapLookupAttributeFromCatalogSupplier
            {
                ChildID = attribute.EntityID,
                EntityID = supplier.EntityID
            }).Result.Attributes
                .DoesNotContain(attribute);
        }

        [Test]
        public void Can_Manage_Manager_Relations()
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;

            #endregion

            var manager = Processor.Execute(new CreateCatalogManager
            {
                AccountID = account.EntityID
            }).Result;
            var supplier = Processor.Execute(new CreateCatalogSupplier
            {
                FileGroupID = 0
            }).Result;

            Processor.Execute(new MapCatalogManagerToCatalogSupplier
            {
                ChildID = manager.EntityID,
                EntityID = supplier.EntityID
            }).Result.Managers
                .DoesContain(manager);

            Processor.Execute(new UnmapCatalogManagerFromCatalogSupplier
            {
                ChildID = manager.EntityID,
                EntityID = supplier.EntityID
            }).Result.Managers
                .DoesNotContain(manager);
        }
    }
}
